import { request } from "@/utils/request";
import { save } from "@/utils/Storage";
import { Toast } from "@ant-design/react-native";
import { action, flow, observable } from "mobx";
class UserStore {
  @observable userInfo: any;
  @action
  setUserInfo = (info: any) => {
    this.userInfo = info;
  };
  requestLogin = flow(function* (
    this: UserStore,
    phone: string,
    pwd: string,
    callback: (success: boolean) => void
  ) {
    const Toast_login = Toast.loading("登录中...");
    try {
      const params = {
        name: phone,
        pwd: pwd,
      };
      const { data } = yield request("login", params);
      if (data) {
        save("userInfo", JSON.stringify(data));
        this.userInfo = data;
        callback?.(true);
      } else {
        this.userInfo = null;
        callback?.(false);
      }
    } catch {
      this.userInfo = null;
      callback?.(false);
    } finally {
      Toast.remove(Toast_login);
    }
  });
}
// ESM单例导出
export default new UserStore();
